/*
 * Amkai Enterprise Solution (AES.) 
 * Copyright 2004 by Amkai(TM), Inc.
 * www.amkai.com
 * 
 * Project: webservice
 * Package: com.amkai.aes.webservice.binary.objecthandler.dictionary
 * File: IsPhysStaffPersonIdExist.java
 * Created: 2007.10.17.9:38:14
 * Author: Keri Kornel
 * 
 */
package com.amkai.aes.webservice.binary.objecthandler.dictionary;

import java.io.Serializable;
import java.util.List;
import java.util.Set;

import com.amkai.aes.aescommon.service.binary.job.Job;
import com.amkai.aes.aescommon.service.binary.job.dictionary.IsPhysStaffPersonIdExistJob;
import com.amkai.aes.generatedbeans.dictionary.ItemKind;
import com.amkai.aes.generatedbeans.physstaff.PhysStaff;
import com.amkai.aes.generatedbeans.registration.Person;
import com.amkai.aes.webservice.binary.JobObjectHandler;
import com.amkai.common.extensions.hibernate.config.SiteManager;
import com.amkai.common.extensions.hibernate.config.manager.SessionManager;

/**
 * @author Keri Kornel
 *
 */
public class IsPhysStaffPersonIdExistJobHandler implements JobObjectHandler <IsPhysStaffPersonIdExistJob, Serializable, Boolean> {

	public IsPhysStaffPersonIdExistJobHandler() {
	}

	/* (non-Javadoc)
	 * @see com.amkai.aes.webservice.binary.JobObjectHandler#process(com.amkai.aes.aescommon.service.binary.job.Job, java.io.Serializable)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Boolean process(IsPhysStaffPersonIdExistJob job, Serializable object) throws Exception {
		if ((job.checkKind==ItemKind.STAFF) || (job.checkKind==ItemKind.PHYSICIAN)) {
			Boolean retval=new Boolean(false);
			SessionManager sm = SiteManager.Start();
			try {
				sm.openSession();
				List<Person> personlist =sm.execute("FROM Person WHERE id="+job.personId.longValue()).list();
				if (personlist.size()>0) {
					for (Person p: personlist) {
						Set<PhysStaff> physStaffs = p.getPhysStaffs();
						for (PhysStaff physStaff:physStaffs) {
							if ((physStaff.getKind()==job.checkKind)) {
								// KKORNEL: AES-12411
								if (physStaff.getBaseOrganization().getId().longValue()==job.getModuleDepartment().getId().longValue()) {
									retval=new Boolean(true);
									break;
								}
							}
						}
					}
				}
			}
			finally {
				sm.closeSession();
				return retval;
			}
		} else return new Boolean(false);
	}

}
